package DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Hibernate.HibernateUtil;
import Negocio.Sucursal;

public class HibernateSucursalDAO {
	private static HibernateSucursalDAO instancia = null;
	private static SessionFactory sf = null;
	
	
	public static HibernateSucursalDAO getInstancia(){
		if(instancia == null){
			instancia = new HibernateSucursalDAO();
			sf = HibernateUtil.getSessionFactory();
		}
		return instancia;
	}
	
	
	
	public void grabarSucursal(Sucursal sucursal){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(sucursal);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch(Exception e){
			System.out.println(e);
			System.out.println("ErrorDAO: SucursalDAO.grabarSucursal");
		}
	}
	
	public void updateSucursal(Sucursal sucursal){
		try{
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(sucursal);
		session.flush();
		session.getTransaction().commit();
		session.close();
		}catch (Exception e){
			System.out.println(e);
			System.out.println("ErrorDAO: SucursalDAO.updateSucursal");
		}
		
	}

	
	public Sucursal getSucursal(int idSucursal){
        try {
        	Session session=sf.openSession();
            session.beginTransaction();
            Sucursal sucursal = (Sucursal) session.createQuery("from Sucursal suc left join fetch suc.envios where suc.idSucursal = ?").setInteger(0, idSucursal).uniqueResult();
            session.getTransaction().commit();
            session.close();
            return sucursal;
        } catch (Exception e){
            System.out.println(e);
            System.out.println("ErrorDAO: SucursalDAO.getSucursal");
        }
        return null;
	}
	
	
	public List<Sucursal> getSucursales() {
		List<Sucursal> lista = null;
		try{
			Session session=sf.openSession();
            session.beginTransaction();
            lista = session.createQuery("from Sucursal").list();
            session.getTransaction().commit();
            session.close();
            return lista;
		}catch(Exception e){
			System.out.println(e);
			System.out.println("ErrorDAO: SucursalDAO.getSucursales");
		}
		return lista;
		
	}

	public Sucursal getSucursalConDireccion(int idDireccion) 
	{
		try
		{
			Session session=sf.openSession();
            session.beginTransaction();
        
            Query q = session.createQuery("select s from Sucursal s join s.direccion d where d.idDireccion = ?").setInteger(0, idDireccion);
            
            Sucursal s = (Sucursal) q.uniqueResult();
            session.getTransaction().commit();
            session.close();
            return s;
		}
		catch(Exception e)
		{
			System.out.println(e);
			System.out.println("ErrorDAO: SucursalDAO.getSucursales");
		}
		return null;
	}

	public Sucursal getSucursalConCalle(String calle) 
	{
		try
		{
			Session session=sf.openSession();
            session.beginTransaction();
        
            Query q = session.createQuery("select s from Sucursal s left join s.direccion d where d.calle = ?").setString(0, calle);
            
            Sucursal s = (Sucursal) q.uniqueResult();
            session.getTransaction().commit();
            session.close();
            return s;
		}
		catch(Exception e)
		{
			System.out.println(e);
			System.out.println("ErrorDAO: SucursalDAO.getSucursales");
		}
		return null;
	}
	
}
